<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Model\Category;
use Illuminate\Http\Request;

class CategoryController extends Controller
{
    public $parentCategory;

    public function __construct()
    {
        $category = Category::withoutTrashed()->get(['id','title','pid']);
        $this->parentCategory = ListToTree($category);
    }

    public function index()
    {
        $categorys = Category::withoutTrashed()->get();

        $tree = ListToTree($categorys->toArray());

        return view('admin.category.category',['tree'=>$tree]);
    }

    public function getCategory(Request $request)
    {
        var_dump($request);
//        return view('admin.category');
    }

    public function create()
    {
        return view('admin.category.categoryadd',['parentCategory'=>$this->parentCategory]);
    }

    public function store(Request $request)
    {
        $param = $request->all();
        $params = getParams($param,array('title','title_en','pid','is_show','_token'));

        $result = Category::create($params);

        if(!$result){
            return response()->json(['code'=>-100,'msg'=>'操作失败，请重试']);
        }

        return response()->json(['code'=>0,'msg'=>'操作成功！']);

    }

    public function edit()
    {
        return view('admin.category.categoryadd',['parentCategory'=>$this->parentCategory]);
    }

    public function save(Request $request)
    {
        var_dump($request);
    }

    public function destory(Request $request)
    {
        var_dump($request);
    }
}
